<?php

/**
 * This file is accessed through JavaScript and executes code on the server side (AJAX).
 * Loads modules (leftmenu, admin menu, centerarea)
 * 
 * @author Siim Loog
 */






session_start();
require_once 'init.php';

if ( !isset($_GET['action']) ) {
	exit;
}

if(empty($_SESSION)){
	echo "LoggedOut";
	exit;
}


$user = $_SESSION['userId'];

switch( $_GET['action'] ) {
	
	// load the menu items for the left menu
	case 'usermenu': 
		echo $site->fetch( 'leftmenu/test.html' );
		break;
		
		
	// returns the appropriate content for the admin menu
	case 'adminOptions':
		$user = User::loadById( $sql, $_SESSION['userId'] );
		if($user) {
			if( $user->oppejoud == 1 ) {
				$_SESSION['oppejoud'] = true;
				echo $site->fetch( 'adminarea/adminstuff.html' );
			} else {
				echo '';
			}
		} else {
		 	echo 'Error';
		}
		break;
		
	// generates the html for centerarea
	case 'loadAllTests':
		$result = Test::loadAll($sql);
		echo '<div class="testHeader">
			<div class="testAttribute">Kontrollt&ouml;&ouml; nimi
			</div>
			
			<div class="testAttribute">Olek
			</div>
			</div>';
		if($result){
			foreach ($result as $value) {
				echo '<div class="testEntry"><div class="testAttribute">';
				echo $value['kt_nimi'];
				echo '</div><div class="testAttribute">';
				if($value['avatud']!=0){
					echo "Avatud";
				} else echo "Suletud";
				echo '</div><div class="testAttribute"><div class="editButton"  onclick="changeState(';
				echo $value['idkontrolltoo'];
				echo ');">Muuda olekut</div></div>';
				echo '<div class="testAttribute"><div class="editButton"  onclick="getTestData(';
				echo $value['idkontrolltoo'];
				echo ');">Redigeeri</div></div>';
				echo '<div class="testAttribute"><div class="editButton"  onclick="deleteTest(';
				echo $value['idkontrolltoo'];
				echo ');">Kustuta</div>';
				
				echo '</div></div>';
			}
		}
		echo '<div class="addButton" onclick="getTestAdder();">Lisa kontrollt&ouml;&ouml;
			</div>';
		
		break;
	
	
	// generates the html for centerarea
	case 'loadAllUsers':
		$result = User::loadAll($sql);
		echo '<div class="userHeader">
			<div class="userAttribute">Matrikkel
			</div>
			<div class="userAttribute">Nimi
			</div>
			<div class="userAttribute">Ruhm
			</div>
			<div class="userAttribute">Roll
			</div>
			<div class="userAttribute">Aktiivne
			</div>
			
		</div>';
		if($result){
			foreach ($result as $value) {
				echo '<div class="userEntry">
						<div class="userAttribute">';
				echo $value['matrikkel'];
				echo '</div><div class="userAttribute">';
				echo $value['perenimi']." ".$value['eesnimi'];
				echo '</div><div class="userAttribute">';
				echo $value['ruhm'];
				echo '</div><div class="userAttribute">';
				if($value['oppejoud']!=0){
					echo "&Ouml;ppej&ouml;ud";
				} else echo "&Ouml;pilane";
				echo '</div><div class="userAttribute">';
							if($value['aktiivne']!=0){
					echo "Aktiivne";
				} else echo "Deaktiveeritud";
	
				echo '</div><div class="userAttribute"><div class="editButton" onclick="getUserData('.$value['idkasutaja'].')">Redigeeri</div></div>
				<div class="userAttribute"><div class="editButton" onclick="loadUserResults('.$value['idkasutaja'].')">Sooritused</div></div></div>';
			}
		}
		break;
	
	
	// generates the html for centerarea
	case 'loadAllResults':
		$result = Result::loadAll($sql);
		echo '<div class="resultHeader">
				<div class="resultAttribute">Kontrollt&ouml;&ouml;
				</div>
				<div class="resultAttribute">Matrikkel
				</div>
				<div class="resultAttribute">Nimi
				</div>
				<div class="resultAttribute">Tulemus
				</div>
				
				</div>';
		if($result){
			foreach ($result as $value) {
				echo '<div class="resultEntry">
					<div class="resultAttribute">';
				echo $value['kt_nimi'];
				echo '</div>
					<div class="resultAttribute">';
				echo $value['matrikkel'];
				echo '</div>
					<div class="resultAttribute">';
				echo $value['eesnimi']." ".$value['perenimi'];
				echo '</div>
					<div class="resultAttribute">';
				echo $value['tulemus'];
				echo '</div>
					<div class="resultAttribute">';
				
				if($value['hinnatud']){
					echo "Hinnatud";
				} else echo "Hindamata";
				echo '</div>
					<div class="resultAttribute">
						<div class="editButton" onclick="loadResult(';
						
				echo $value['idsooritus'];
				echo ');">Vaata
						</div>
					</div>
					</div>';
				
			}
		}
		break;
		
		
	// Return the data of selected user for editing
	case 'getUserData':
		$id = addslashes(trim($_GET['user']));
		$student = User::loadById($sql, $id);
		echo '<div class="attributeEditor"><p>
			Id:<br />
			<input type="text" name="userId" readonly id="userId" class="email" value="';
		echo $student->id;
		echo '"/>
			</p>
		
		<p>
			Matrikkel:<br />
			<input type="text" name="matrikkel" id="matrikkel" class="email" value="';

		echo $student->matrikkel;
		echo '"/>
		</p>
		<p>
			Eesnimi:<br />
			<input type="text" name="firstName" id="firstName" class="email" value="';
		echo $student->eesnimi;
		echo '"/>
			</p>
			<p>
				Perenimi:<br />
				<input type="text" name="lastName" id="lastName" class="email" value="';
		echo $student->perenimi;
		echo '"/>
			</p>
			<p>
				R&uuml;hm:<br />
				<input type="number" min="1" max="10" step="1" value="';
				
		echo $student->ruhm;		
		echo '" name="group" id="group"/>
			</p>
			<p>
				<button onclick="editUserData();" class="userEditButton">Muuda</button>
			</p>
			<p>
			<button onclick="getPasswordEditingArea();" class="userEditButton">Muuda parooli</button>
			</p>';
		
		if($student->aktiivne){
			echo '
			
			
			<p>
			<button onclick="deActivateUser();" class="userEditButton">Deaktiveeri</button>
			</p>';
		} else echo '<p>
			<button onclick="activateUser();" class="userEditButton">Aktiveeri</button>
			</p>';
		
		echo '
		<p>
			<button onclick="deleteUser();" class="userEditButton">Kustuta</button>
		</p></div>';
		echo '<div id="passwordEditor" class="passwordEditor"></div>';
				
		break;
		
	// Return html of open tests
	case 'loadOpenTests':
		$result = Test::load($sql);
		echo '<div class="testHeader">
			<div class="testAttribute">Kontrollt&ouml;&ouml; nimi
			</div>
			<div class="testAttribute">Katseid lubatud
			</div>
			<div class="testAttribute">Olek
			</div>
			</div>';
		if($result){
			foreach ($result as $value) {
				
				$finished = Test::checkIfFinished($sql, $value['idkontrolltoo']);
				if(!$finished){
					$testName = $value['kt_nimi'];
					echo '<div class="testEntry"><div class="testAttribute">';
					echo $value['kt_nimi'];
					echo '</div><div class="testAttribute">';
					echo $value['lubatud_katseid'];
					echo '</div><div class="testAttribute">';
					if($value['avatud']!=0){
						echo "Avatud";
					} else echo "Suletud";
					echo '</div><div class="testAttribute" style="width: 38%;"><div class="editButton"  onclick="solve('.$value['idkontrolltoo'].');">Lahendama</div></div></div>';
					}
			}
		}
				
		break;
		
	// Return html of this users tests
	case 'loadMyResults':
		$result = Result::loadActiveUserResults($sql);
		echo '<div class="resultHeader">
				<div class="resultAttribute">Kontrollt&ouml;&ouml;
				</div>
				<div class="resultAttribute">Punkte
				</div>
				<div class="resultAttribute">Vaata
				</div>
				
				</div>';
		if($result){
			foreach ($result as $value) {
				echo '<div class="resultEntry">
					<div class="resultAttribute">';
				echo $value['kt_nimi'];
				echo '</div>
					<div class="resultAttribute">';
				echo $value['tulemus'];
				echo '</div>
					<div class="resultAttribute">
						<div class="editButton" onclick="viewResult(&#34;';
						
				echo $value['idsooritus'];
				echo '&#34;);">Vaata
						</div>
					</div>
					</div>';
				
			}
		}
				
		break;
		
		
	// Return html of this users tests
	case 'loadUserResults':
		$result = Result::loadUserResults($sql,$_GET['userId']);
		echo '<div class="resultHeader">
				<div class="resultAttribute">Kontrollt&ouml;&ouml;
				</div>
				<div class="resultAttribute">Punkte
				</div>
				<div class="resultAttribute">Matrikkel
				</div>
				<div class="resultAttribute">Nimi
				</div>
				<div class="resultAttribute">Hinnatud
				</div>
				<div class="resultAttribute">
				</div>
				
				</div>';
		if($result){
			foreach ($result as $value) {
				echo '<div class="resultEntry">
					<div class="resultAttribute">';
				echo $value['kt_nimi'];
				echo '</div>
					<div class="resultAttribute">';
				echo $value['tulemus'];
				echo '</div>
					<div class="resultAttribute">';
				echo $value['matrikkel'];
				echo '</div>
					<div class="resultAttribute">';
				echo $value['perenimi']." ".$value['eesnimi'];
				echo '</div>
				<div class="resultAttribute">';
				if($value['hinnatud']){
					echo "Hinnatud";
				} else echo "Hindamata";
				echo '</div>
					<div class="resultAttribute">
						<div class="editButton" onclick="loadResult(&#34;';
						
				echo $value['idsooritus'];
				echo '&#34;);">Vaata
						</div>
					</div>
					</div>';
				
			}
		}
				
		break;
		
	case 'loadEntityTest':
			echo $site->fetch('centerarea/entity.html');	
		break;
		
		
		
	// Return questions of test
	case 'loadTest':
		//test id
		$testId = $_GET['testId'];
		$_SESSION['testId'] = $testId;
		
		
		
		$userId = $_SESSION['userId']; 
		$result = Problem::problemDescription($sql, $testId);
		$previousData = Result::checkIfExists( $sql, $userId, $testId );
		$resultId = Result::getId($sql, $userId, $testId);
		$_SESSION['sooritus'] = $resultId;

		
		if(!$result){
			echo "Kontrolltöös pole ülesandeid";
			break;
		}
		$entryNr = 1;
		echo "<p>";
		echo '<button class="answerButton" onclick="endResult('.$_SESSION['sooritus'].');" >Lõpeta</button>';
		echo "<br/></p>";
		if($result){
			foreach ($result as $value) {
				$identifier = "entity".strval($value['idulesanne']);
				//print_r($result);	
				echo '<div class="problem">
						<div class="problemDescription">
						'.$entryNr.'.<br/>	
						<p>Ülesanne: ';
				echo ''.$value['tekst'].'';
				echo "<br/>Argumendid: ";
				echo $value['argumendid'];
				echo '</p>
						</div>
						<div class="studentGuess"><p>
							Sisesta vastus:<br />
							<textarea spellcheck="false" rows="3" cols="50" name="'.$identifier.'" id = "'.$identifier.'"></textarea>
						</div>
						<div class="buttonPad">
							<button onclick="insertAtCursor(\''.$identifier.'\',\'∀\');" class="s11">∀</button>
							<button onclick="insertAtCursor(\''.$identifier.'\',\'∃\');" class="s11">∃</button>
							<button onclick="insertAtCursor(\''.$identifier.'\',\'¬\');" class="s11">¬</button>
							<button onclick="insertAtCursor(\''.$identifier.'\',\'⊃\');" class="s11">⊃</button>
							<button onclick="insertAtCursor(\''.$identifier.'\',\'⋁\');" class="s11">⋁</button>
							<button onclick="insertAtCursor(\''.$identifier.'\',\'&\');" class="s11">&</button>
						</div>
						
						<button onclick="checkAnswer(';
				echo $value['idulesanne'];
				echo ');" class="answerButton">Vasta</button>
						<div id="answerStatus'.$value['idulesanne'].'" class="answerStatus">';
				foreach($previousData as $old){
					$exists = array_key_exists('ulesanne', $old);
					if($exists){
						if($old['ulesanne'] == $value['idulesanne']){
							echo "Vastatud: ".html_entity_decode($old['vastus']);
						}
					}
				}
				echo '</div></div>';
				
				$entryNr++;
				
			}
		}
		echo "<p>";
		echo '<button class="answerButton" onclick="endResult('.$_SESSION['sooritus'].');" >Lõpeta</button>';
		echo "<br/></p>";
		echo "<br/>";
		break;
	
	case 'questionAdder':
		$id = addslashes(trim($_GET['testId']));
		$testName = Test::loadName($sql, $id);
		if($testName){
			echo 'Kontrolltöö: '.$testName.'<br/>';
			echo 'Lisa ülesanne<br/>';	
			echo '<div class="questionAdder">';		
			echo $site->fetch('centerarea/qadder.html');	
			echo '</div>';
			echo '<p>
				<button onclick="insertProblem('.$id.');" class="centerButton">Sisesta</button>
				</p>';
			echo '<div id="statusArea" class="statusArea"></div>';
		}
		break;
		
	case 'testName':
		$id = addslashes(trim($_GET['testId']));
		
		break;
		
	}
	
	
	
?>
